﻿using IPCLibrary;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace IPCLibraryTest
{
    
    
    /// <summary>
    ///This is a test class for MailslotTest and is intended
    ///to contain all MailslotTest Unit Tests
    ///</summary>
	[TestClass()]
	public class MailslotTest
	{


		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion


		/// <summary>
		/// Mailslot Test
		/// </summary>
		[TestMethod()]
		public void MailslotMessageTest()
		{
			Mailslot target = new Mailslot("TestMailslot");
			target.Create();
			string expected = "This is a test";
			string actual;

			MailslotClient client = new MailslotClient("TestMailslot");

			client.SendMessage(expected);
			actual = target.ReadMessage();

			target.Close();
			client.Close();

			Assert.AreEqual(expected, actual, "Message was not send/received successfully.");
		}
		[TestMethod()]
		public void MailslotMessageTest2()
		{
			Mailslot server = new Mailslot("TestMailslot");
			server.Create();

			MailslotClient client = new MailslotClient("TestMailslot");

			string expected = "This is a test";
			string actual;

			client.SendMessage(expected);
			actual = server.ReadMessage();

			server.Close();
			client.Close();

			Assert.AreEqual(expected, actual, "Message was not send/received successfully.");
		}
		[TestMethod()]
		public void MailslotMessageTest3()
		{
			string expected = "This is a test";
			string actual;

			Mailslot server = new Mailslot("TestMailslot");
			server.Create();

			// Send a few messages from two clients.
			MailslotClient client1 = new MailslotClient("TestMailslot");
			MailslotClient client2 = new MailslotClient("TestMailslot");

			client1.SendMessage(expected);
			client2.SendMessage(expected);
			client1.SendMessage(expected);
			client2.SendMessage(expected);

			// Read back the 4 messages
			actual = server.ReadMessage();
			Assert.AreEqual(expected, actual, "Message was not send/received successfully.");
			actual = server.ReadMessage();
			Assert.AreEqual(expected, actual, "Message was not send/received successfully.");
			actual = server.ReadMessage();
			Assert.AreEqual(expected, actual, "Message was not send/received successfully.");
			actual = server.ReadMessage();
			Assert.AreEqual(expected, actual, "Message was not send/received successfully.");

			server.Close();
			client1.Close();
			client2.Close();
		}
	}
}
